﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AIMLbot
{
    public class RememberPlugin : Plugins.IPlugin
    {
        #region IPlugin Members
        public string Name { get { return "AI Learning"; } }
        public List<string> Command
        {
            get { return new List<string>() { "remember" }; }
        }

        public string RootStore { get; set; }

        public MSNPSharp.Messenger Messenger { get; set; }
        public string Chat(string Command, string UserID, string Input)
        {
            //remember that FOO BAR IS/ARE SOMETHING....
            string rawInput = Input.Trim();
            string lower = rawInput.ToLower().Trim();
            if (lower.StartsWith("that"))
            {
                rawInput = rawInput.Substring("that".Length);
                lower = lower.Substring("that".Length);

                int finalIndex = -1;
                int isIndex = lower.IndexOf(" is ");
                int areIndex = lower.IndexOf(" are ");
                if (isIndex > 0 || areIndex > 0)
                {
                    finalIndex = isIndex;
                    if (isIndex > areIndex && areIndex > 0) finalIndex = areIndex;
                    string key = rawInput.Substring(0, finalIndex).Trim();
                    string value = rawInput;// rawInput.Substring(finalIndex).Trim();
                    WriteAIML(key, value);
                }
            }
            return "";
        }
        protected void WriteAIML(string Key, string Value)
        {
            string aimlTemplate = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?><aiml><category><pattern>* {0} *</pattern><template>{1}</template></category></aiml>";
            string final = string.Format(aimlTemplate, Key, Value);
            string aimlGuid = System.Guid.NewGuid().ToString();
            string fileName =
                System.IO.Path.Combine(
                        System.IO.Path.Combine(
                                System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), "AIML"),
                                aimlGuid + ".aiml"
                                );
            System.IO.File.WriteAllText(fileName, final);
        }
        
        #endregion
    }
}
